package com.yangfan.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yangfan.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping(value = "hystrix80")
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/ok/{id}")
    public String ok (@PathVariable(value = "id") Long id){

        return paymentService.ok(id);

    };


    /**
     * 下面的 被注销了，因为默认这个类的 注解上配置了默认的
     * @param id
     * @return
     */
    @GetMapping(value = "/error/{id}")
    @HystrixCommand(fallbackMethod = "errorFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "15000")//设置超过多少秒就报错
    })
    public String error(@PathVariable(value = "id") Long id){

        return paymentService.error(id);

    };


    /**
     * 被降级调用的方法
     * @param id
     * @return
     */
    public String errorFallbackMethod(Long id,Throwable e)
    {
        log.info("我是客户端兜底方法运行了");
        return   "线程池" + Thread.currentThread().getName() + "我是客户端!等了 15 秒 还没好 ！ 报错了！！！！兜底方法运行了啊啊啊 id="+id + "\t"+e ;
    }


}
